<?php
include_once(dirname(__FILE__)."/../inc/common.php");
$method = isset($_GET["method"])?$_GET["method"]:"";

$flowClass = new tally_flow();

if($method=="listQry") {
    // 支出
    $dateFrom = isset($_POST["dateFrom"])&&$_POST["dateFrom"]?strtotime($_POST["dateFrom"]):strtotime("1970-01-01");
    $dateTo = isset($_POST["dateTo"])&&$_POST["dateTo"]?strtotime($_POST["dateTo"]):time();
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $tagName = isset($_POST["tagName"])?$_POST["tagName"]:"";
    $showInvisible = isset($_GET["showInvisible"]) && ($_GET["showInvisible"]==1)?1:0;

    $tallyTagClass = new tally_tag();
    $tagList = $tallyTagClass->findTagByAccountId($userId, $tagName, $showInvisible);

    $results = array();
    $tagIds = $tagIds?explode(",", $tagIds):array();
    foreach ($tagList as $tag){
        if(count($tagIds)==0 || in_array($tag["id"], $tagIds)){
            $expense = $flowClass->getCountByTimeRange($userId,
                tally_flow::$INOUT_EXPENSE, $dateFrom, $dateTo, $tag["id"], $categoryId);
            $income = $flowClass->getCountByTimeRange($userId,
                tally_flow::$INOUT_INCOMME, $dateFrom, $dateTo, $tag["id"], $categoryId);
            $results[] = array(
                "name" => $tag["name"],
                "expense" => abs($expense),
                "income" => abs($income),
                "sum" => $income - abs($expense)
            );
        }
    }
    ?>
    <!--begin:数据列表-------------------------------------------------------------------------------------------------->
    <table id="listtb" class="listtable" cellpadding="0" cellspacing="1">
        <thead>
        <tr>
            <th width="40%">标签名称</th>
            <th width="20%">收入</th>
            <th width="20%">支出</th>
            <th width="20%">净收入</th>
        </tr>
        </thead>
        <tbody>


        <?php
        foreach($results as $row) {
            if($row["income"]==0 && $row["expense"]==0){
                continue;
            }
        ?>
        <tr>
            <td ><?php echo $row["name"]?></td>
            <td><?php echo number_format($row["income"], 2, ".", "");?></td>
            <td><?php echo number_format($row["expense"], 2, ".", "");?></td>
            <td><?php echo number_format($row["sum"], 2, ".", "");?></td>
        </tr>
        <?php
        }
        ?>

        </tbody>
        <tfoot>
        <tr>
            <td colspan="4"  class="to_left">
                (注：按最新汇率,以人民币计)
            </td></tr>
        </tfoot>
    </table>
    <!--end:数据列表区-------------------------------------------------------------------------------------------------->
    <?php
}else{
    ?>
    <!-- begin:导航条 -->
    <div class="titlebar">
        <h3>按标签统计分析</h3>
    </div>
    <!-- end:导航条 -->

    <div class="tab_wrapper">
        <div class="body">
            <!-- begin:搜索区 -->
            <form id="schForm" name="schForm" method="post">
                <div class="searchbar" id ="searchbar">
                    <div class="base_condition">
                        <table>
                            <tr>
                                <td><SELECT id ="dateSpan" NAME="dateSpan" onChange="updateDate('dateSpan','dateFrom','dateTo');">
                                        <option value="" selected>日期</option>
                                        <option value="today">今日</option>
                                        <option value="theWeek">本周</option>
                                        <option value="theMonth">本月</option>
                                        <option value="theQuarter">本季度</option>
                                        <option value="theYear">今年</option>
                                        <option value="lastWeek">上周</option>
                                        <option value="lastMonth">上月</option>
                                        <option value="lastQuarter">上季度</option>
                                        <option value="lastYear">去年</option>
                                        <option value="last7Day">近7天</option>
                                        <option value="last30Day">近30天</option>
                                        <option value="custom">自定义</option>
                                    </SELECT>
                                    从<INPUT class="Wdate date" TYPE="text" id="dateFrom" NAME="dateFrom" onFocus="WdatePicker();" value="" onChange="updateType('dateSpan');">
                                    至<INPUT class="Wdate date" TYPE="text" id="dateTo" NAME="dateTo" onFocus="WdatePicker();" value="" onChange="updateType('dateSpan');">
                                </td>
                                <td>
                                    <label for="tagName">标签:</label>
                                    <input type="text" class="text m rd" id="tagName" name="tags" readonly="readonly" onclick="CM.clearValue(this);">
                                    <input type="hidden" value="" name="tagId" id="tagId"/>
                                    <a title="选择标签" href="#" onclick="TagPicker.open(this,'1');return false;"><img src="/images/icons/select.gif" align="absmiddle"/></a>
                                </td>
                                <td><input id="searchBtn" class="button" type="button" value="查询" onclick="TagReport.search();"/></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </form>
            <!-- end:搜索区 -->
            <!-- begin:-------主数据区 -------------------->
            <div id="dataView" class="data">
                <div  align="center"><img align="absmiddle" src='/images/common/loading1.gif'></div>
            </div>
            <!--end: ----------主数据区----------------->
        </div>
    </div>
    <?php
}
?>